<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/拖拽.css"/>
	</head>
	<body>
		<div id="box">
			
		</div>
	</body>
	<script type="text/javascript">
		var box = document.getElementById("box");
		document.addEventListener("touchstart",startFn,false);
		document.addEventListener("touchmove",moveFn,false);
		var disX = 0;
		var disY = 0;
		var disLeft = 0;
		var disTop = 0;
		function startFn(event){
			if(event.touches.length){
				var touch = event.touches[0];
			}
			disX = touch.clientX;
			disY = touch.clientY;
			disLeft = box.offsetLeft;
			disTop = box.offsetTop;
		}
		function moveFn(event){
			event.preventDefault();
			if(event.touches.length){
				var touch = event.touches[0];
			}
			var endX = touch.clientX - disX;
			var endY = touch.clientY - disY;
			var x = endX + disLeft;
			var y = endY + disTop;
			var maxX = window.innerHeight - box.offsetWidth;
			var maxY = window.innerHeight - box.offsetHeight;
			if(x >= maxX){
				x = maxX;
			}else if(x <= 0){
				x = 0;
			}
			if (y >= maxY) {
				y = maxY;
			} else if(y <= 0){
				y = 0;
			}
			box.style.left = x + "px";
			box.style.top = y + "px";
		}
	</script>
</html>
